package com.khadi.testcase.view.model;

import java.util.LinkedList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class WorkFlowDtlTableModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6124636492220246051L;

	private String[] columns;
	private List<WorkflowDtlModel> data;

	public WorkFlowDtlTableModel(String[] columns) {
		this.columns = columns;
	}

	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return this.columns.length;
	}

	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return this.columns[column];
	}

	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return getData().size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		WorkflowDtlModel model = getValueAt(rowIndex);
		if (model != null) {
			switch (columnIndex) {
			case 0:
				return model.getDescription();
			case 1:
				return model.getCommandAction();
			case 2:
				return model.getHtmlFieldModel().getName();
			case 3:
				return model.getHtmlFieldModel().getType();
			case 4:
				return model.getSortOrder();

			default:
				break;
			}
		}

		return null;
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		if (value != null) {
			WorkflowDtlModel model = getValueAt(rowIndex);
			if (model != null) {
				switch (columnIndex) {
				case 0:
					model.setDescription((String) value);
					break;
				case 1:
					model.setCommandAction((String) value);
					break;
				case 4:
					model.setSortOrder((Integer) value);
					break;
				default:
					break;
				}
			}
			fireTableRowsUpdated(rowIndex, rowIndex);
		}
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {

		return true;
	}

	private WorkflowDtlModel getValueAt(int rowIndex) {
		return getData().get(rowIndex);
	}

	public void setData(List<WorkflowDtlModel> data) {
		this.data = data;
		fireTableDataChanged();
	}

	public List<WorkflowDtlModel> getData() {
		if (this.data == null) {
			this.data = new LinkedList<WorkflowDtlModel>();
		}
		return data;
	}

}
